package cn.edu.cjw.APMFP.ui.dialog;

import cn.edu.cjw.APMFP.BOMTable.Service.BTService;
import cn.edu.cjw.APMFP.BOMTable.Service.BTServiceImpl;
import cn.edu.cjw.APMFP.BOMTable.pojo.BOMTable;
import cn.edu.cjw.APMFP.Product.Servicve.ProductService;
import cn.edu.cjw.APMFP.Product.Servicve.ProductServiceImpl;
import cn.edu.cjw.APMFP.Product.pojo.Product;
import cn.edu.cjw.APMFP.Util.StringUtil;
import cn.edu.cjw.APMFP.WorkStep.Service.WsService;
import cn.edu.cjw.APMFP.WorkStep.Service.WsServiceImpl;
import cn.edu.cjw.APMFP.WorkStep.pojo.WorkStep;
import cn.edu.cjw.APMFP.ui.component.PlaceholderTextField;
import cn.edu.cjw.APMFP.ui.component.combobox.JFilterComboBox;
import cn.edu.cjw.APMFP.ui.component.combobox.MultiComboBox;
import cn.edu.cjw.APMFP.ui.table.TableCallBack;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;


public class ProductDialog extends JDialog {

	private JButton confirmBtn = new JButton("确认");
	private JButton cancelBtn = new JButton("取消");

	private ProductService service = new ProductServiceImpl();

	private WsService wsService = new WsServiceImpl();

	private BTService btService = new BTServiceImpl();

	private Product entity;

	private TableCallBack callBack;

	private List<BOMTable> bom1List = new ArrayList<>();
	private List<BOMTable> bom2List = new ArrayList<>();
	private List<WorkStep> workStepList = new ArrayList<>();

//	private List<JFilterComboBox> filterList = new ArrayList<>();
	private List<JComboBox<String>> filterList = new ArrayList<>();
	private List<PlaceholderTextField> countList = new ArrayList<>();

//	private List<JFilterComboBox> filter2List = new ArrayList<>();
	private List<JComboBox<String>> filter2List = new ArrayList<>();
	private List<PlaceholderTextField> count2List = new ArrayList<>();

	private int sCount;
	private int wCount;

	public ProductDialog(Product entity, TableCallBack callBack){
		this.sCount = 1;
		this.wCount = 1;
		this.entity = entity;
		this.callBack = callBack;
		this.setTitle(entity != null ? "编辑" : "新增");
		this.setSize(700, 900);
		this.init();
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
//		this.pack();
		this.setVisible(true);
	}

	private void init(){
		this.setLayout(new BorderLayout());

		try {
			bom1List.addAll(btService.searchByType("成品"));
			bom2List.addAll(btService.searchByType("原料"));
			workStepList.addAll(wsService.showAll());
		} catch (Exception e) {
			e.printStackTrace();
		}

		JPanel centerPanel = new JPanel(new BorderLayout(10, 10));
		JPanel pPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
		pPanel.setBorder(BorderFactory.createTitledBorder("成品"));
		pPanel.setPreferredSize(new Dimension(500, 100));
		JComboBox<String> pField = new JComboBox<>();
		bom1List.forEach(b -> pField.addItem(b.getBName()));
		pField.setPreferredSize(new Dimension(300, 30));
		pPanel.add(pField);

		JScrollPane wPanel = new JScrollPane();
		JPanel wwPanel = new JPanel(new GridBagLayout());
		wPanel.setBorder(BorderFactory.createTitledBorder("原料"));
		wPanel.setPreferredSize(new Dimension(500, 200));
		initGridPanel(wwPanel);
		wPanel.setViewportView(wwPanel);

		JScrollPane sPanel = new JScrollPane();
		JPanel ssPanel = new JPanel(new GridBagLayout());
		sPanel.setBorder(BorderFactory.createTitledBorder("工序"));
		sPanel.setPreferredSize(new Dimension(500, 200));
		initGridPanel2(ssPanel);
		sPanel.setViewportView(ssPanel);

		centerPanel.add(pPanel, BorderLayout.NORTH);
		centerPanel.add(wPanel, BorderLayout.CENTER);
		centerPanel.add(sPanel, BorderLayout.SOUTH);


		this.add(centerPanel, BorderLayout.CENTER);

		if(entity != null){
			pField.setSelectedItem(entity.getBName());
		}

		JPanel footerPanel = new JPanel();
		footerPanel.setPreferredSize(new Dimension(this.getWidth(), 100));
		footerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 10));
		footerPanel.add(confirmBtn);
		footerPanel.add(cancelBtn);
		confirmBtn.addActionListener(e -> {
			int pIndex = pField.getSelectedIndex();
			if(pIndex == -1){
				JOptionPane.showMessageDialog(null, "请选择成品");
				return;
			}

			HashMap<String, Long> materialMap = new HashMap<>();
			for(int i = 0; i < filterList.size(); i++){
				int i1 = filterList.get(i).getSelectedIndex();
				if(i1 == -1){
					JOptionPane.showMessageDialog(null, "请选择原料");
					return;
				}
				String id = bom2List.get(i1).getBId();
				String countStr = countList.get(i).getText();
				if(!StringUtil.isInt(countStr)){
					JOptionPane.showMessageDialog(null, "请输入合适数量");
					return;
				}
				materialMap.put(id, Long.valueOf(countStr));
			}

			HashMap<Integer, String> stepMap = new HashMap<>();
			for(int i = 0; i < filter2List.size(); i++){
				int i1 = filter2List.get(i).getSelectedIndex();
				if(i1 == -1){
					JOptionPane.showMessageDialog(null, "请选择工序");
					return;
				}
				String id = workStepList.get(i1).getWId();
				stepMap.put(i + 1, id);
			}

			try {
				if(entity == null){
					service.addProduct(bom1List.get(pIndex), materialMap, stepMap);
				}else{
					service.updateProductMaterial(entity, materialMap);
					service.updateProductWorkstep(entity, stepMap);
				}
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null, "操作失败:" + ex.getMessage());
				return;
			}
			if(callBack != null){
				callBack.refreshTable();
			}
			JOptionPane.showMessageDialog(null, "操作成功");
			close();
		});
		cancelBtn.addActionListener(e -> {
			close();
		});
		this.add(footerPanel, BorderLayout.SOUTH);
	}

	/**
	 * 物料
	 * @param centerJPanel
	 */
	public void initGridPanel(JPanel centerJPanel){
		centerJPanel.removeAll();
		filterList.clear();
		countList.clear();
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		constraints.ipady = 10;
		for(int i = 0; i < wCount; i++){
			final int index = i;
//			JFilterComboBox pField = new JFilterComboBox(bom2List.stream().map(BOMTable::getBName).toArray());
			JComboBox<String> pField = new JComboBox(bom2List.stream().map(BOMTable::getBName).toArray());
			pField.setPreferredSize(new Dimension(200, 30));
			PlaceholderTextField cFiled = new PlaceholderTextField("", 27);
			cFiled.setPlaceholder("数量");
			JButton addBtn = new JButton(i == 0 ? "+" : "-");
			constraints.gridx = 0;
			constraints.gridy = i;
			centerJPanel.add(pField, constraints);
			constraints.gridx = 1;
			centerJPanel.add(cFiled, constraints);
			constraints.gridx = 2;
			centerJPanel.add(addBtn, constraints);
			filterList.add(pField);
			countList.add(cFiled);

			addBtn.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if(index == 0){
						wCount++;
						initGridPanel(centerJPanel);
					}else{
						wCount--;
						initGridPanel(centerJPanel);
					}
				}
			});
		}
		centerJPanel.updateUI();
		centerJPanel.repaint();
	}

	/**
	 * 工序
	 * @param centerJPanel
	 */
	public void initGridPanel2(JPanel centerJPanel){
		centerJPanel.removeAll();
		filter2List.clear();
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		constraints.ipady = 10;
		for(int i = 0; i < sCount; i++){
			final int index = i;
//			JFilterComboBox pField = new JFilterComboBox(workStepList.stream().map(WorkStep::getWName).toArray());
			JComboBox<String> pField = new JComboBox(workStepList.stream().map(WorkStep::getWName).toArray());
			pField.setPreferredSize(new Dimension(200, 30));
//			PlaceholderTextField cFiled = new PlaceholderTextField("", 27);
//			cFiled.setPlaceholder("数量");
			JButton addBtn = new JButton(i == 0 ? "+" : "-");
			constraints.gridx = 0;
			constraints.gridy = i;
			centerJPanel.add(pField, constraints);
//			constraints.gridx = 1;
//			centerJPanel.add(cFiled, constraints);
			constraints.gridx = 1;
			centerJPanel.add(addBtn, constraints);
			filter2List.add(pField);
//			count2List.add(cFiled);

			addBtn.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if(index == 0){
						sCount++;
						initGridPanel2(centerJPanel);
					}else{
						sCount--;
						initGridPanel2(centerJPanel);
					}
				}
			});
		}
		centerJPanel.updateUI();
		centerJPanel.repaint();
	}
	
	private void close(){
		this.dispose();
	}

	public static void main(String[] args) {
		new ProductDialog(null, null);
	}
}
